<?php

class View_Helper{
	private $HEADER = null;
	private $display = false;
	

	/**
	 * Build a link in view page
	 * 
	 * @param string $label
	 * @param array $array_params
	 * @param array $attributes
	 */
	public function link($label,$array_params,$attributes = array()){
		$url = Nucleo_Controller_Controller::url($array_params);

		$att = '';
		foreach($attributes as $key=>$value){
			$att .= $key.'="'.$value.'"  ';
		}
		
		echo '<a href="'.$url.'" '.$att.' >'.$label.'</a>';
	}
	
	/**
	 * Responsable for add stylesheet
	 * 
	 * @param String Stylesheet name
	 * @param Boolean TRUE para em PUBLIC_PATH ou FALSE para VIEW ou pasta externa
	 * @param Boolean true se o arquivo nao estiver nas pastas padrões de estilos
	 *
	 */
	public function addStyle($nameStyle,$public = false,$external = false){
		if(!$external){
			$file = $this->getPath('css',$public).$nameStyle;
			$BASE_APPLICATION = BASE_APPLICATION;
		}else{
			$file = $nameStyle;
			$BASE_APPLICATION = '';
		}
			
		if(file_exists($file) || $external )
			$this->HEADER .= "<link rel='stylesheet' href='".$BASE_APPLICATION.$file."' />";		
		else
			throw new Nucleo_Exception_Style($nameStyle);		
	}
	
	/**
	 * Responsable for add scripts
	 *  
	 * @param String  Nome do Script
	 * @param Boolean TRUE para em PUBLIC_PATH ou FALSE para VIEW ou pasta externa
	 * @param Boolean true se o arquivo nao estiver nas pastas padrões de scripts
	 * @param String elemento TYPE da tag html script
	 */
	public function addScript($nameScript,$public = false,$external = false,$type='text/javascript'){
		if(!$external){
			$file = $this->getPath('js',$public).$nameScript;
			$BASE_APPLICATION = BASE_APPLICATION;
		}else{
			$file = $nameScript;
			$BASE_APPLICATION = '';
		}
		
		if(file_exists($file) || $external)
			$this->HEADER .= '<script src="'.$BASE_APPLICATION.$file.'" type="'.$type.'"></script>';
		else
			throw new Nucleo_Exception_Javascript($nameScript);		
	}
	
	/**
	 * Will add images
	 * 
	 * @param String Nome da Imagem
	 * @param String ID for tag HTML
	 * @param Array Options for img
	 * @param Boolean TRUE for PUBLIC_PATH. FALSE for VIEW or path external
	 * @param Boolean TRUE if file no is in default path of styles
	 *
	 */
	public function addImg($nameImg,$id,$arrayOptions = NULL,
							$public = false,$external = false){
		if(!$external)
			$file = $this->getPath('img',$public).$nameImg;
		else
			$file = $nameImg;
		
		$options = '';
		if(isset($arrayOptions))
			foreach($arrayOptions as $option=>$value)
				$options .= $option.':'.$value.';';
		
		if(file_exists($file))
			$this->HEADER .= "<style> div#".$id."{ background: url('"
						   .BASE_APPLICATION.$file."') no-repeat; 
							".$options."
							} </style>";		
		else
			throw new Nucleo_Exception_Style($nameImg);		
	}
	
	/**
	 * Will return the path location VIEW
	 * 
	 * @param string nome da constante a ser localizada
	 * @param boolean caso se refira a pasta publica
	 * @return string
	 */
	public function getPath($path,$public = false){
		$path = strtoupper($path).'_PATH';
		
		if($public){
			$path = MODULE_PATH.'View/'.constant($path);
		}else{
			$path = MODULE_PATH
				  . Nucleo_Controller_Factory::$modulo.'/View/'.constant($path);
		}
		
		return $path;
	}
	
	/**
	 * Return the tags of the Html Header
	 * 
	 * @return $string tags for head tag
	 */
	public function getHeader(){
		return $this->HEADER;
	}
}